package com.bajiao.admin.controller.agreement;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.bajiao.common.api.vo.Result;
import com.bajiao.entity.agreement.Agreement;
import com.bajiao.service.agreement.AgreementService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.bajiao.admin.base.AdminBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 协议表
 * @Author: FanJq
 * @Date:   2024-12-23
 * @Version: V1.0
 */
@Slf4j
@Api(tags="协议表")
@RestController
@RequestMapping("/agreement")
public class AgreementController extends AdminBaseController<Agreement, AgreementService> {
	@Autowired
	private AgreementService agreementService;

	/**
	 * @description: 分页列表查询
	 * @author: FanJq
	 * @date:   2024-12-23
	 * @return
	 */
	@ApiOperation(value="协议表-分页列表查询", notes="协议表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(String title, String code) {
		LambdaQueryWrapper<Agreement> queryWrapper = new LambdaQueryWrapper<>();
        startPage();
		queryWrapper.like(StrUtil.isNotBlank(title), Agreement::getTitle, title);
		queryWrapper.like(StrUtil.isNotBlank(code), Agreement::getCode, code);
		queryWrapper.orderByDesc(Agreement::getCreateTime);
        return Result.ok(getDataTable(agreementService.list(queryWrapper)));
	}

	/**
	 * @description: 添加
	 * @author: FanJq
	 * @date:   2024-12-23
	 * @param agreement
	 * @return
	 */
	@ApiOperation(value="协议表-添加", notes="协议表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Agreement agreement) {
		Integer count = agreementService.lambdaQuery().eq(Agreement::getCode, agreement.getCode().trim()).count();
		if (count > 0)
			return Result.error("编码已存在，请更换编码信息");
		agreementService.save(agreement);
		return Result.ok("添加成功！");
	}

	/**
	 * @description: 编辑
	 * @author: FanJq
	 * @date:   2024-12-23
	 * @param agreement
	 * @return
	 */
	@ApiOperation(value="协议表-编辑", notes="协议表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Agreement agreement) {
		agreementService.updateById(agreement);
		return Result.ok("编辑成功!");
	}

	/**
	 * @description: 通过id删除
	 * @author: FanJq
	 * @date:   2024-12-23
	 * @param id
	 * @return
	 */
	@ApiOperation(value="协议表-通过id删除", notes="协议表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		agreementService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * @description: 批量删除
	 * @author: FanJq
	 * @date:   2024-12-23
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="协议表-批量删除", notes="协议表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.agreementService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * @description: 通过id查询
	 * @author: FanJq
	 * @date:   2024-12-23
	 * @param id
	 * @return
	 */
	@ApiOperation(value="协议表-通过id查询", notes="协议表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Agreement agreement = agreementService.getById(id);
		return Result.ok(agreement);
	}

  /**
   * @description: 导出excel
   * @author: FanJq
   * @date:   2024-12-23
   * @param request
   * @param agreement
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, Agreement agreement) {
      return super.exportXls(request, agreement, Agreement.class, "协议表");
  }

  /**
   * @description: 通过excel导入数据
   * @author: FanJq
   * @date:   2024-12-23
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, Agreement.class);
  }

}
